import pytesseract as ts
import re
import xlwt
import os
import cv2
from PIL import Image
from PIL import ImageFilter
img_fn='./pic/ab.jpg'
lang='chi_sim'
#锐化图片
imageObject = Image.open(img_fn)   
sharpened1 = imageObject.filter(ImageFilter.SHARPEN)
sharpened2 = sharpened1.filter(ImageFilter.SHARPEN)
#创建表格文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
#创建一张表单
sheet = book.add_sheet('核酸',cell_overwrite_ok=True)
#表头写入
sheet.write(0,0,'姓名')
sheet.write(0,1,'采样地址')
sheet.write(0,2,'采样时间')
sheet.write(0,3,'采样结果')
#循环变量控制写入的行
i = 1
#从文件夹中读取图片进行识别、写入
for root ,dirs ,files in os.walk("./pic",topdown=False):
	for file in files:
		img = cv2.imread("./pic/"+file,0)
		#cv2.imwrite('gray-bin.jpg',img)
		#text=ts.image_to_string(img,lang)
		"""
		text=ts.image_to_string(img,lang)
		arry1 = re.findall(r"姓 名 :(.*)",text)
		arry2 = re.findall(r"采 样 点 :  (.*)",text)
		arry3 = re.findall(r"采 样 时 间 ,(.*)",text)
		arry4 = re.findall(r"检 测 结 果 :(.*)",text)
		"""



print(img_fn)
img = cv2.imread(img_fn,0)
cv2.imwrite('gray-bin.jpg',img)
text=ts.image_to_string(img,lang)
print(text)
arry1 = re.findall(r"姓 名(.*)",text)
arry2 = re.findall(r"采 样 点(.*)",text)
arry3 = re.findall(r"采 样 时 间(.*)",text)
arry4 = re.findall(r"检 测 结 果(.*)",text)
print(arry2[0])
print(arry3[0])
print(arry4[0])
#创建表格文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
#创建一张表单
sheet = book.add_sheet('核酸',cell_overwrite_ok=True)
sheet.write(1,0,arry1[0])
sheet.write(1,1,arry2[0])
sheet.write(1,2,arry3[0])
sheet.write(1,3,arry4[0])
sheet.write(0,0,'姓名')
sheet.write(0,1,'采样地址')
sheet.write(0,2,'采样时间')
sheet.write(0,3,'采样结果')
savepath='./d.xls'
book.save(savepath)
